<!DOCTYPE html>
<html><head></head>

<body>

<style type="text/css">
  body { margin: auto; width: 100%; }
  .title { margin: 20; padding: 20; font-size: 42; font-weight: bold; text-align: center; }
  div.centre {
    background: gray;
    width: 700px;
    display: block;
    margin-left: auto;
    margin-right: auto;
  }
</style>

<div class="title">IDL Generator Test</div>
<p>
<textarea cols=100 rows=40 id="result"></textarea>

<script>
// Test Event Handler
function onDemoEvent(event) {
  var output = document.getElementById("result");
  if (typeof event.msg === 'undefined' ||
      typeof event.msg.data === 'undefined') {
    output.value += 'Not expected, received: ' + JSON.stringify(event) + '\n';
  } else {
    output.value += 'onDemoEvent(): ' + event.msg.data + '\n';
  }
}

window.onload = function() {
  if (typeof window.idl_demo === 'undefined') {
    document.title = 'Fail';
    console.log("no idl_demo support");
    return;
  }
  var errorCallBack = function(error) {
    document.title = 'Fail';
    console.log(error);
  }

  // Test Event Handler
  idl_demo.addEventListener('demoevent', onDemoEvent);

  // Test Method
  idl_demo.hi("Hello World!").then(function(result) {
    var output = document.getElementById("result");
    output.value += "hi(): " + result + '\n';

    // Test attribute getter
    output.value += "Test getting attrWritable:\n expecting '29', gets: " + idl_demo.attrWritable + '\n';
    output.value += "Test getting attrReadOnly:\n expecting 'hello', gets: " + idl_demo.attrReadOnly + '\n';

    // Test self-defined JS
    idl_demo.calculate(3, 6).then(function(result) {
      var output = document.getElementById("result");
      output.value += "calculate() result: (3+6)*2 = " + result + '\n';

      // Test attribute setter
      idl_demo.setProperty("attrWritable", 35);
      idl_demo.setProperty("attrReadOnly", "Set by JS");
      output.value += "Testing writting attrWritable:\n expecting '35', gets: " + idl_demo.attrWritable + '\n';
      output.value += "Testing writting attrReadOnly:\n expecting 'hello', gets: " + idl_demo.attrReadOnly + '\n';
    }, errorCallBack);
  }, errorCallBack);

}
</script>

</body></html>
